layui.use( function(){
	var table = layui.table;
  var form = layui.form;
  var element = layui.element;

  table.render({
    elem: '#test'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
    ,height: 'full-168' //高度最大化减去差值
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,defaultToolbar: ['filter', 'exports', 'print']
    ,title: '用户数据表'
    ,cols: [[
      {field: 'numbers', title: '序号', type:'numbers', align:'center', width: 68}
      ,{field:'id', title: 'ID', align: 'center', sort: true, filter: true}//单元格内容水平居中
      ,{field:'title', title: '名称', align: 'center', sort: true, filter: true}//单元格内容水平居中
      ,{field:'url', title: '链接', align: 'center', sort: true, filter: true}
      ,{field:'description', title: '描述', align: 'center', sort: true, filter: true}
      ,{field:'keywords', title: '关键词', align: 'center', sort: true, filter: true}
      ,{field:'type', title: '类型', align: 'center', sort: true, filter: true}
      ,{field:'state', title: '状态', align: 'center', sort: true, filter: true}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
    ]]
    ,data: []
    ,page: true//开启分页
    ,limits: [10,50,100,200,500,5000]
  });
  
  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.alert('确定要删除如下数据么?<br><br>名称:['+data.title+']<br><br>链接: ['+data.url+']<br><br>描述: ['+data.description+']<br><br>关键字: ['+data.keywords+']',{icon:3,title:'谨慎操作[不可逆]',btn: ['确认','取消']}, function(index){
        $.getJSON("./del.php?jsoncallback=?&del_id="+data.id+"&token="+$("#token").val(), function(data) {
          if (data.status != "NO") {
            obj.del();
            layer.closeAll('');
            layer.alert(data.message, {icon: 6,title:"删除成功"});
          } else {
            layer.alert(data.message, {icon: 5,title:"删除失败"});
          }
        });
      });
    } else if(obj.event === 'edit'){
      if (data.state == "隐藏") {
        var url_state = false;
      } else {
        var url_state = true;
      }
    	form.val('example', {
        "url_title": data.title // "name": "value"
        ,"url_url": data.url
        ,"url_description": data.description
        ,"url_keywords": data.keywords
        ,"url_state": url_state //开关状态
        ,"url_type": data.type
        ,"url_id": data.id
      });
    }
  });

  $("#url_url").blur(function(){
    $.get("./get_title.php?url="+$('#url_url').val(),function(data,status){
      obj = JSON.parse(data);
      if (obj.status != "YES") {
        layer.msg("返回数据: " + data + "<br>链接状态: " + status);
      }else {
        form.val('example', {
          "url_title": obj.title // "name": "value"
          ,"url_description": obj.description
          ,"url_keywords": obj.keywords
        });
      }
    });
  });

  form.on('submit(0)', function(data){
    //console.log(JSON.stringify(data.field));
    table.reload('test', {page: {curr: 1},data: []});//执行重载
    layer.msg('数据拼命加载中,数据处理需要一些时间,<br>等待时间视网络及数据量变化,请您耐心等候...', { icon: 16,shade: 0.1 });
    $.getJSON("./get.php?jsoncallback=?&title="+data.field.url_title+"&url="+data.field.url_url+"&description="+data.field.url_description+"&keywords="+data.field.url_keywords+"&type="+data.field.url_type+"&state="+data.field.url_state+"&token="+data.field.token, function(data) {
      if (data.status != "NO") {
        layer.closeAll('');
        table.reload('test', {page: {curr: 1},data: data});//执行重载
      }else {
        layer.closeAll('');
        layer.alert(data.message, {icon: 5,title:"错误！"});
      }
    });
  });

  form.on('submit(1)', function(data){
    //console.log(JSON.stringify(data.field));
    if (data.field.url_url.replace(/(^\s*)|(\s*$)/g, "") == "") {
      layer.alert("提交的数据不能为空，请检查后重试！", {icon: 5,title:'请填写 URL'}, function(){layer.closeAll('');});
      return;
    } else if (data.field.url_title.replace(/(^\s*)|(\s*$)/g, "") == "") {
      layer.alert("提交的数据不能为空，请检查后重试！", {icon: 5,title:'请填写 名称'}, function(){layer.closeAll('');});
      return;
    } else if (data.field.url_description.replace(/(^\s*)|(\s*$)/g, "") == "") {
      layer.alert("提交的数据不能为空，请检查后重试！", {icon: 5,title:'请填写 描述'}, function(){layer.closeAll('');});
      return;
    } else if (data.field.url_keywords.replace(/(^\s*)|(\s*$)/g, "") == "") {
      layer.alert("提交的数据不能为空，请检查后重试！", {icon: 5,title:'请填写 关键字'}, function(){layer.closeAll('');});
      return;
    } else if (data.field.url_type.replace(/(^\s*)|(\s*$)/g, "") == "") {
      layer.alert("提交的数据不能为空，请检查后重试！", {icon: 5,title:'请选择数据类型'}, function(){layer.closeAll('');});
      return;
    } else if (data.field.token.replace(/(^\s*)|(\s*$)/g, "") == "") {
      layer.alert("提交的数据不能为空，请检查后重试！", {icon: 5,title:'请填写 口令'}, function(){layer.closeAll('');});
      return;
    } else {
      $.getJSON("./up.php?jsoncallback=?&title="+data.field.url_title+"&url="+data.field.url_url+"&description="+data.field.url_description+"&keywords="+data.field.url_keywords+"&type="+data.field.url_type+"&state="+data.field.url_state+"&token="+data.field.token, function(datas) {
        if (datas.status != "NO") {
          layer.closeAll('');
          layer.alert(datas.message, {icon: 6,title:"成功！"});
          table.reload('test', {page: {curr: 1},data: datas.data});//执行重载
        } else {
          layer.closeAll('');
          layer.alert(datas.message, {icon: 5,title:"失败！"});
        }
      });
    }
  });
});